// pages/company/index.js
const app = getApp()
var core = require('../../utils/core.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
     list:[],
      page:1,
      fresh:false,


      navigation_flag:false,
      navigation_index_1:-1,
      navigation_index_2:-1,
      navigation_index_3:-1,
      navigation_index_4:-1,
      city_id:'',
      chain_id:0,
      id:0,
      order:'',   

      navigation:[],
      VipFlag:true,
  },
  onRefresh(){
    var self=this
    
    self.setData({fresh:true,list:[],page:1})
    // setTimeout(function(){
    //   self.setData({fresh:false})
    // },1000)
    this.getList()
  },
  more:function(){
    // console.log(1222)
     this.getList()
   },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options != null&& options!= undefined&&options.openid) {
      wx.setStorageSync('otheropenid', options.openid)  
    }
    if(options.id!=undefined){
        this.setData({chain_id:options.id,id:options.id})
    }
    this.GetChoose()
    this.onRefresh()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.ChainFree();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
   /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return this.share()
  },
    /**
   * 用户点击右上角分享
   */
  onShareTimeline: function () {
    return this.share()
  },
  share:function(){
    return {
      title:  '产业链大数据企业数据查询',
      path: '/pages/company/list?chain_id='+this.data.chain_id+'&openid=' +wx.getStorageSync('openid1'),
      imageUrl:  ''//自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径。支持PNG及JPG。显示图片长宽比是 5:4。
    }
  },
  
  GetChoose(){
    var self=this
    core.post("Common",{op:'type1'},function(res){
      console.log(res)
      if(res.code==1){
          self.setData({
            navigation:res.data,
          })   
      }
    })
  },
  
  /**
   * 选择筛选类型
   */
  choose_type:function(res){
    var data=res.currentTarget.id.split('&&')
    var index=data[1]
    
    this.setData({
      [index]:data[0]
    })
    var level=data[1].split('index_')[1]*1
    
    for (let i =level+1; i <=4; i++) {
      index= 'navigation_index_'+i
      console.log(index)
      this.setData({
        [index]:-1
      }) 
    }
    console.log(this.data)
    //把本级true  下级变成false
    var self=this
    

    var data_lin=[];
    var data_str=''
    var data_arr=[];
    if(level*1==1){
      data_lin=self.data.navigation;
      for (let i = 0; i < data_lin.length; i++) {
        if(i==data[0]) {
            if(data_lin[i].flag==false){
              data_lin[i].flag=true
              this.setData({navigation_flag:1})
            }else{
              data_lin[i].flag=false
              this.setData({navigation_flag:0})
            }
        } else{
           data_lin[i].flag=false;
        }     
      }
      // data_str=JSON.stringify(data_lin[self.data.navigation_index_1].children)
      // data_arr=JSON.parse(data_str.replace(/"flag":true/g, '"flag":false'))
      // data_lin[self.data.navigation_index_1].children=data_arr
      // data_lin[self.data.navigation_index_1].children[data[0]].flag=true
      //self.setData({navigation:data_lin})

      self.setData({navigation:data_lin})
     
    }
    if(level*1==2){
      data_lin=self.data.navigation;
      data_str=JSON.stringify(data_lin[self.data.navigation_index_1].children)
      data_arr=JSON.parse(data_str.replace(/"flag":true/g, '"flag":false'))
      data_lin[self.data.navigation_index_1].children=data_arr
      data_lin[self.data.navigation_index_1].children[data[0]].flag=true
      self.setData({navigation:data_lin})
    }
    if(level*1==3){
      data_lin=self.data.navigation;
      data_str=JSON.stringify(data_lin[self.data.navigation_index_1].children[self.data.navigation_index_2].children)
      data_arr=JSON.parse(data_str.replace(/"flag":true/g, '"flag":false'))
      data_lin[self.data.navigation_index_1].children[self.data.navigation_index_2].children=data_arr
      data_lin[self.data.navigation_index_1].children[self.data.navigation_index_2].children[data[0]].flag=true
      self.setData({navigation:data_lin})
      console.log(data_lin)
    }
    if(level*1==4){
      data_lin=self.data.navigation;
      data_str=JSON.stringify(data_lin[self.data.navigation_index_1].children[self.data.navigation_index_2].children[self.data.navigation_index_3].children)
      data_arr=JSON.parse(data_str.replace(/"flag":true/g, '"flag":false'))
      data_lin[self.data.navigation_index_1].children[self.data.navigation_index_2].children[self.data.navigation_index_3].children=data_arr
      data_lin[self.data.navigation_index_1].children[self.data.navigation_index_2].children[self.data.navigation_index_3].children[data[0]].flag=true
      self.setData({navigation:data_lin})
      console.log(data_lin)
    }
   
  },
  choose_con:function(target){
    var id=target.currentTarget.id;
    var self=this
    var data=self.data.navigation;
    if(id==0){
       data[self.data.navigation_index_1].flag=false;
       this.setData({navigation:data,navigation_flag:false})
    }
    if(id==1){
      
      var name=[];
      var ids=[];


      if(self.data.navigation_index_2!=-1&&data[self.data.navigation_index_1].children[self.data.navigation_index_2]){
        name[name.length]=data[self.data.navigation_index_1].children[self.data.navigation_index_2].name
        ids[ids.length]=data[self.data.navigation_index_1].children[self.data.navigation_index_2].id
      }
      if(self.data.navigation_index_3!=-1&&data[self.data.navigation_index_1].children[self.data.navigation_index_2].children[self.data.navigation_index_3]){
        name[name.length]=data[self.data.navigation_index_1].children[self.data.navigation_index_2].children[self.data.navigation_index_3].name
        ids[ids.length]=data[self.data.navigation_index_1].children[self.data.navigation_index_2].children[self.data.navigation_index_3].id
      }
      if(self.data.navigation_index_4!=-1&&data[self.data.navigation_index_1].children[self.data.navigation_index_2].children[self.data.navigation_index_3].children[self.data.navigation_index_4]){
        name[name.length]=data[self.data.navigation_index_1].children[self.data.navigation_index_2].children[self.data.navigation_index_3].children[self.data.navigation_index_4].name
        ids[ids.length]=data[self.data.navigation_index_1].children[self.data.navigation_index_2].children[self.data.navigation_index_3].children[self.data.navigation_index_4].id
      }
      data[self.data.navigation_index_1].flag=false;
      data[self.data.navigation_index_1].name=name.join(",");
    
      var data_str=JSON.stringify(data)
      var data_arr=JSON.parse(data_str.replace(/"flag":true/g, '"flag":false'))
      
      //城市搜索
      if(self.data.navigation_index_1==0&&ids.length>0){
          this.setData({city_id:ids[ids.length-1]})
      }
      //标签搜索
      if(self.data.navigation_index_1==1&&ids.length>0){
        this.setData({chain_id:ids[ids.length-1]})
      }
      //排序
      if(self.data.navigation_index_1==2&&ids.length>0){
        this.setData({order:ids[ids.length-1]})
      }
      self.setData({navigation:data_arr,navigation_flag:false,navigation_index_1:-1,navigation_index_2:-1,navigation_index_3:-1,navigation_index_4:-1})

      this.setData({list:[],page:1})
      this.getList()

   }
  },
    /**
   * list
   */
  getList:function(){
    wx.showLoading({
      title: '加载中',
    })
    var self=this
    var data={
      chain_id:self.data.chain_id,
      page:self.data.page,
      op:'list'
    }
    console.log(data)
    core.post("Company",data,function(res){
      wx.hideLoading();
      var data=self.data.list
      if(res.code==1){
        if(res.data.length>0){
          for (let i = 0; i < res.data.length; i++) {
             
              data.push(res.data[i])             
          }

          self.setData({list:data,page:self.data.page+1})
        }else{
          wx.showToast({
            'title':'没有更多了',
            'icon':'none'
          })
        }
        self.setData({fresh:false})
         wx.stopPullDownRefresh()
      }
     
     
    })
  },
    /**
   * 获得用户信息
   */
  GetUserInfo:function(){
    var self=this
    core.post("GetUserinfo",{},function(res){
      if(res.code==1){
           self.setData({                     
             VipFlag: res.data.IsVip 
           })
      }
    })
  },
  /**
   * 产业链是否免费
   */
  ChainFree:function(){
    var self=this
    if(self.data.id!=0){
      core.post("label",{op:'OnlineChain',chain_id:self.data.id},function(res){
        console.log(res)
        if(res.code==1&&res.data.current.fee==0){
          self.setData({                     
            VipFlag: true
          })
        }else{
           self.GetUserInfo()
        }
      })
    }
  },
  ToBuy:function(){
    wx.navigateTo({
      url: '/pages/person/vip/open',
    })
  },
  ConcelPay:function(){
    wx.navigateBack({
      delta: 1
    })
  }
  
})